"""
    Tests for Partner gate
    This tests can stub the models.DataAccess
    class methods with test_models.FakeDataAccess methods
"""
from pgate.gate import create_app
from pgate.models import DataAccess
from test_base import PartnerGateBaseTestCase
import unittest


class StubTests(PartnerGateBaseTestCase):
    def test_non_mdf5_hash(self):
        """Test a 32 char url that is not a valid md5"""
        response = self.app.get("/12435/zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")
        self.assert200(response)
        self.assertEqual(response.data, "0")

    def test_callback_feature(self):
        """Test the callback feature of partner gate."""
        self.stub.Set(DataAccess, 'get_url', lambda self, p, u: "1317266723")
        response = self.app.get("/12435/e018e2d49783e212c38c33f69fed6c01" \
                                "?callback=alert")
        self.assert200(response)
        self.assertEquals("alert(1317266723);\n", response.data)

    def test_bogus_url(self):
        """Test a bogus url"""
        response = self.app.get("/12435/23423432")
        self.assert200(response)
        self.assertEqual(response.data, "0")

    def test_bogus_url2(self):
        """Test a second  bogus url"""
        response = self.app.get("/doug")
        self.assert200(response)
        self.assertEqual(response.data, "0")

    def test_same_timestamp(self):
        """Test everytime you call with a specific url the same
           timestamp is returned."""
        self.reset_stubs()
        first = self.assert_url_response("50001",
                                         "e018e2d49783e212c38c33f69fed6c01")
        import time
        time.sleep(1)

        second = self.assert_url_response("50001",
                                          "e018e2d49783e212c38c33f69fed6c01")
        self.assertEqual(first, second)

    def test_total(self):
        """Test the total functionality"""
        self.reset_stubs()
        self.assert_url_response("50001",
                                 "a018e2d49783e212c38c33f69fed6c01")
        response = self.assert_total_response("50001")
        self.assertEquals(response, "2")

        self.assert_url_response("12345",
                                 "e018e2d49783e212c38c33f69fed6c01" \
                                 "?callback=alert")
        response = self.assert_total_response("12345")
        self.assertEquals(response, "1")

if __name__ == "__main__":
    unittest.main()
